<?php

namespace core;

class imooc
{

    public static $classMap = array();
    public $assgin = array();
    public static function run()
    {
        $route = new \core\lib\route();
        $ctrlClass = $route->ctrl;
        $action = $route->action;
        $ctrlfile = APP.'/ctrl/'.$ctrlClass.'Ctrl.php';
        $ctrlClass = '\\app\ctrl\\'.$ctrlClass.'Ctrl';
        if(is_file($ctrlfile)){
            include $ctrlfile;
            $ctrl = new $ctrlClass();
            $ctrl->$action();
        }else{
            throw new \Exception('不存在该控制器');
        }
    }

    public static function load($class)
    {
        // 自动加载类库
        $class = str_replace('\\','/',$class);
        $file = IMOOC.'/'.$class.'.php';
        include $file;
    }

    public function assign($name,$value)
    {
        $this->assign[$name] = $value;
    }

    public function display($file)
    {
        $path = APP.'/views/'.$file;
        if(is_file($path)){
            $loader = new \Twig\Loader\FilesystemLoader(APP.'/views');
            $twig = new \Twig\Environment($loader,[
                'cache' => IMOOC.'/log',
                'debug' => DEBUG,
            ]);
            $template = $twig->loadTemplate($file);
            $template->display($this->assign?$this->assign:[]);
        }
    }

}